﻿

$dropFolder = "\\tsclient\E\PowerShellDpeScratch\output"

###
### Find the SSRS path
###
if (Test-Path "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSRS10.MSSQLSERVER\Setup")
{
    $SSRSInstallFolder = (Get-ItemProperty -path "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSRS10.MSSQLSERVER\Setup").SQLpath
}
elseif (Test-Path "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSRS10_50.MSSQLSERVER\Setup")
{
    $SSRSInstallFolder = (Get-ItemProperty -path "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server\MSRS10_50.MSSQLSERVER\Setup").SQLpath
}


###
### Find the VisualStudio path
### 
if (Test-Path "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Microsoft SQL Server\100\Tools\ClientSetup")
{
    $SqlClientSetupFolder = (Get-ItemProperty -path "HKLM:\SOFTWARE\Wow6432Node\Microsoft\Microsoft SQL Server\100\Tools\ClientSetup").VSToolsPath
    
    Copy-Item -Force (Join-Path $dropFolder PowerShellDataProcessingExtension.dll) $SqlClientSetupFolder
    Copy-Item -Force (Join-Path $dropFolder PowerShellDataProcessingExtension.pdb) $SqlClientSetupFolder
}


$RSBinFolder = "$SSRSInstallFolder" + "ReportServer\bin\"
$RSReportServerConfig = "$SSRSInstallFolder" + "ReportServer\rsreportserver.config"
$RSSrvPolicyConfig = "$SSRSInstallFolder" + "ReportServer\rssrvpolicy.config"
$VSPrivateAssembliesFolder = "C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\PrivateAssemblies"
$RSDesignerConfig = Join-Path $VSPrivateAssembliesFolder RSReportDesigner.config
$RSPreviewPolicy = Join-Path $VSPrivateAssembliesFolder RSPreviewPolicy.config

Copy-Item -Force (Join-Path $dropFolder PowerShellDataProcessingExtension.dll) $RSBinFolder
Copy-Item -Force (Join-Path $dropFolder PowerShellDataProcessingExtension.pdb) $RSBinFolder


###
### Update the SSRS rsreportserver.config File
###
if(Test-path $RSReportServerConfig)
{	
	$RSReportingServerConfigXml = [xml] (Get-Content $RSReportServerConfig) 
	$rootReporting = $RSReportingServerConfigXml.Configuration.Extensions.Data;
	#IF its Added Don't Add again
	if($rootReporting.Extension | Where-Object {$_.Name -eq "PowerShell"})
    {		
		Write-Host "PowerShell DPE already added to the rsreportserver.config" -ForegroundColor Green	
	}
	else
	{	
		### Backup the file before we modify it
		Write-Host "Backing up the SSRS RSReportConfig file..." -NoNewline
		Copy-Item $RSReportServerConfig ($RSReportServerConfig + ".bak")
		Write-Host "Done" -ForegroundColor Green
	
		### Modify the SSRS RSReportConfig.config File
	    Write-Host "Modifying the SSRS RSReportConfig file..." -NoNewline
		$dpe = $RSReportingServerConfigXml.CreateElement("Extension");
		$dpe.SetAttribute("Name","PowerShell")
		$dpe.SetAttribute("Type","PowerShellDataProcessingExtension.PSDBConnectionExtension, PowerShellDataProcessingExtension")
		[Void]$rootReporting.AppendChild($dpe);
		$RSReportingServerConfigXml.Save($RSReportServerConfig);
        Write-Host "DPE added into rsreportserver.config" -ForegroundColor Green
	}
}
else
{
    Write-Host "SSRS Config File Not Found at the following path: " -ForegroundColor Red
    Write-Host $RSReportServerConfig
}

###
### Update the SSRS rsreportserver.config File
###
if(Test-path $RSDesignerConfig)
{	
	$RSDesignerConfigXml = [xml] (Get-Content $RSDesignerConfig) 
	$rootReporting = $RSDesignerConfigXml.Configuration.Extensions.Data;
	#IF its Added Don't Add again
	if($rootReporting.Extension | Where-Object {$_.Name -eq "PowerShell"})
    {		
		Write-Host "PowerShell DPE already added to the rsreportserver.config" -ForegroundColor Green	
	}
	else
	{	
		### Backup the file before we modify it
		Write-Host "Backing up the SSRS RSReportDesigner.config file..." -NoNewline
		Copy-Item $RSDesignerConfig ($RSDesignerConfig + ".bak")
		Write-Host "Done" -ForegroundColor Green
	
		### Modify the SSRS RSDesignerConfig.config File
	    Write-Host "Modifying the SSRS RSReportDesigner.config file..." -NoNewline
		$dpe = $RSDesignerConfigXml.CreateElement("Extension");
		$dpe.SetAttribute("Name","PowerShell")
		$dpe.SetAttribute("Type","PowerShellDataProcessingExtension.PSDBConnectionExtension, PowerShellDataProcessingExtension")
		[Void]$rootReporting.AppendChild($dpe);
		$RSDesignerConfigXml.Save($RSDesignerConfig);
        Write-Host "DPE added into RSReportDesigner.config" -ForegroundColor Green
	}
}
else
{
    Write-Host "SSRS Config File Not Found at the following path: " -ForegroundColor Red
    Write-Host $RSDesignerConfig
}
